<template>
  <view class='coll-l'>
    <uni-collapse @change='changeStatus'>
      <uni-collapse-item>
        <template v-slot:title>
          <view class="chanshishuju">
            <view class="chanshishuju-to store">
              <view class="none1" style="font-weight:bold;"><text :selectable="true">服务编码：{{item.serviceCode}}</text></view>
              <view class="none2" style="font-weight:bold;">
                {{item.valuationId==null?"未返回预估单":item.projectProgress+"%"}}
              </view>
            </view>
            <view class='store'>服务名称：<text :selectable="true">{{item.serviceName}}</text></view>
            <view class="chanshishuju-buttom type">
              <view>
                <view class="t1">
                  商合预占金额：
                  <text :selectable="true">
                    {{item.shMoney.includes(".")==true?item.shMoney:item.shMoney+".00"}}
                  </text>
                </view>
                <view class="t2">
                  累计验工金额：
                  <text :selectable="true" style="color: red;font-weight:bold;">
                    {{item.sumMoney.includes(".")==true?item.sumMoney:item.sumMoney+".00"}}
                  </text>
                </view>
              </view>
              <view>
                <view class="t1">
<!--                  todo 精度问题-->
                  商合预占数：<text  :selectable="true">{{item.shNum}}</text>
                </view>
                <view class="t2">累计验工数量： <text  :selectable="true">{{item.sumNum}}</text>
                </view>
              </view>
             
            </view>
          </view>
        </template>
        <view class="list-body type">
          <view class="yuanab">
            <view>
              <text class="t1">供应商名称：</text>
              <text :selectable="true">{{item.supplierName}}</text>
            </view>
            <view style='display: flex;justify-content: space-between'>
              <view>
                <text class="t1">订单状态：</text>
                <text  :selectable="true"
                  class="t2">{{orderStatus[item.orderState] || orderStatus['default']}}</text>
              </view>
              <view>
                <text class="t1">单价（元）：</text>
                <text  :selectable="true"
                  class="t2">{{item.price.includes(".")==true?item.price:item.price+".00"}}</text>
              </view>
            </view>
          </view>
        </view>
        <!-- 滑块 -->
        <view class="Slider">
          <view style="background-color:white;margin: 6rpx 0;">
            <u-subsection :list="['基本信息', '计价信息']" @change="accordionChange" activeColor="#f56c6c"
              mode="subsection"></u-subsection>
          </view>
          <!-- 基本信息 -->
          <view class="list-bodyer type" v-show="accordion == false">
            <view><text  :selectable="true" style="word-break: break-all">订单编号：{{item.orderCode}}</text></view>
            <view><text  :selectable="true">合同编码：{{item.contractCode}}</text></view>
            <view><text  :selectable="true">录入计价次数：{{item.valuationDetails.length}}</text></view>
           <view v-if='isEdit'>
             <view class="btn-body" v-if="!['03','04','31','41'].includes(pageType)">
               <button style="font-size: 28rpx;width: 120rpx;height: 50rpx;line-height: 50rpx;" class="btn--l" type="warn" @click="addClick(item.valuationId)">新增</button>
               <button class="btn--l2" @click="chongxiao(item.valuationId)"
                 style="border:1px solid #EB4B4B;color:#EB4B4B ;font-size: 28rpx;width: 120rpx;height: 50rpx;line-height: 50rpx;">冲销</button>
             </view>
             <view class="btn-body" v-else>
               <button style="font-size: 28rpx;width: 200rpx;height: 50rpx;line-height: 50rpx;" class="btn--l" type="warn" @click="chongxiaoNew(item.valuationId)">首付款冲销</button>
             </view>
           </view>
          </view>
          <!-- 计价信息 -->
          <view class="Pricinginformation " v-show="accordion == true">
            <scroll-view :scroll-y='true' style='height: 260rpx' :show-scrollbar="true">
              <!-- item.valuationDetails -->
              <view class="Pricingtion" v-for="(num,index2) in item.valuationDetails" :key="index2">
                <uni-collapse ref="collapseView" style="background-color: #00000010;">
                  <uni-collapse-item>
                    <template v-slot:title>
                      <view class="lister-top">
                        <view class="t1">
                          <text  :selectable="true" space="ensp">第{{index2 +1}}次验工计价{{" "}}</text>
                        </view>
                        <view class="t2">
                          <text   :selectable="true" space="ensp">{{num.createTime}}{{" "}}</text>
                        </view>
                        <view class="t3">
                          <text  :selectable="true">{{num.payType == "FIRST"?"首付款":"进度款"}}</text>
                        </view>
                      </view>
                    </template>
                    <view class="lister-buttom type">
                      <view class="conner">
                        <label class="t1">项目进度：<text  :selectable="true">{{num.projectProgress}}%</text></label>
                        
                        <label class="t2">推送状态：
                          <span :style="getColor(num.isPush)">
																{{num.isPush=='0'?'未推送':num.isPush=='1'?'已推送':num.isPush=='2'?'超时':'部分推送'}}
															</span>
                        </label>
                      </view>
                      <view class="conner">
                        <label class="t1">本期验工数量：<text  :selectable="true">{{Number(num.currentInspectionNum)}}</text></label>
                        <label class="t2">本期验工金额：<text  :selectable="true">{{num.currentInspectionMoney.includes(".")==true?num.currentInspectionMoney:num.currentInspectionMoney+".00"}}</text></label>
                      </view>
                    </view>
                  </uni-collapse-item>
                </uni-collapse>
              </view>
            </scroll-view>
            <view v-if='isEdit'>
              <view class="btn-body"  v-if="!['03','04','31','41'].includes(pageType)">
                <button style="font-size: 28rpx;width: 180rpx;height: 50rpx;line-height: 50rpx;" class="btn--l" type="warn" v-debounce="deleteObj">删除</button>
                <button style="font-size: 28rpx;width: 120rpx;height: 50rpx;line-height: 50rpx;"
                  class="btn--l" type="warn" @click="edit(item.valuationId)">修改</button>
                <button style="font-size: 28rpx;width: 170rpx;height: 50rpx;line-height: 50rpx;" class="btn--l" type="warn" v-debounce="tuisong">推送商合</button>
              </view>
              <view v-else>
<!--                <button style="font-size: 28rpx;width: 180rpx;height: 50rpx;line-height: 50rpx;" class="btn&#45;&#45;l" type="warn" @click="deleteObj()">删除</button>-->
              </view>
            </view>
          </view>
        </view>
      </uni-collapse-item>
    </uni-collapse>
  </view>
</template>
<script>
import {postApi} from "@/inspur/utils/request";

const orderStatus = {
  '1':'有效',
  '0':'无效',
  '-1':'冻结',
  'default':'取消冻结'
}
export default {
  data() {
    return {
      accordion: false,
      orderStatus
    };
  },
  onLoad(e) {
    console.log('组件',e)
  },
  props:['item','pageType', 'isEdit', 'isNew'],
  methods:{
    edit(valuationId){
      const detail = this.item.valuationDetails[this.item.valuationDetails.length-1]
      if (!detail){
        uni.showModal({confirmColor: '#eb4b4b',
          title: '温馨提示',
          content: "没有数据编辑啦",
          showCancel: false,
        })
        return;
      }
      if (detail.isPush != '0'){
        uni.showModal({confirmColor: '#eb4b4b',
          title: '温馨提示',
          content: "已推送或已超时,无法修改",
          showCancel: false,
        })
        return;
      }
      if(Number(detail.currentInspectionNum) > 0){
        uni.navigateTo({
          url: `./addProject2?valuationId=${valuationId}&isNew=${this.isNew}&isEdit=true`
        });
      }else{
        uni.navigateTo({
          url: `./writeOff2?valuationId=${valuationId}&isNew=${this.isNew}&isEdit=true`
        });
      }
      
    },
    changeStatus(){
      setTimeout(()=>{
        this.$emit('resize')
      },300)
    },
    accordionChange(index) {
      this.accordion = index == 0 ? false : true; //true是
    },
    getColor(num) {
      if (num == '0') {
        return 'color:#EB4B4B;'
      } else {
        return 'color:#09BB07;'
      }
    },
    addClick(valuationId) {
      // if (this.item.pid === '0' && this.item.orderState === '1' && this.item.ifAdd === '1') {
      //   // console.log('订单有效状态下并且已经有首付款（含安全生产费）信息显示新增验工计价')
      // } else {
      //   this.$message({
      //     message: '仅支持对订单状态为“有效”的预估单发起新增操作',
      //     type: 'warning'
      //   })
      //   return
      // }
      if (this.item.orderState!=1){
        uni.showModal({confirmColor: '#eb4b4b',
          title: '温馨提示',
          content: "请确认该预估单状态",
          showCancel: false,
        })
        return
      }
      if (this.item.valuationDetails?.length>0){
        const detail = this.item.valuationDetails[this.item.valuationDetails.length-1]
        if (detail.isPush != '1'){
          uni.showModal({confirmColor: '#eb4b4b',
            title: '温馨提示',
            content: "已存在未推送验工计价数据",
            showCancel: false,
          })
          return
        }
      }
      if (Number(this.item.sumNum)>=Number(this.item.shNum)){
          uni.showModal({confirmColor: '#eb4b4b',
            title: '温馨提示',
            content: "当前预估单累计验工数量已满额，不可新增验工计价",
            showCancel: false,
          })
          return
      }
      if (Number(this.item.sumMoney)>=Number(this.item.shMoney)){
        uni.showModal({confirmColor: '#eb4b4b',
          title: '温馨提示',
          content: "当前预估单累计验工金额已满额，不可新增验工计价",
          showCancel: false,
        })
        return
      }
      const params = {
        prjId: this.item.prjId,
        subPrjId: this.item.subPrjId,
        serviceCode: this.item.serviceCode
      }
      postApi('/material/api/insVa/module/selectExistServ',{params}).then(res=>{
        if (res.data?.length>0){
          uni.navigateTo({
            url: `./addProject2?valuationId=${valuationId}&isNew=${this.isNew}&isEdit=false`
          });
        }else{
          uni.showModal({confirmColor: '#eb4b4b',
            title: '温馨提示',
            content: '该服务已经不存在,不支持验工计价',
            showCancel: false,
          })
        }
      })
      
    },
    chongxiao(valuationId) {
      if (this.item.valuationDetails?.length>0){
        const detail = this.item.valuationDetails[this.item.valuationDetails.length-1]
        if (detail.isPush != '1'){
          uni.showModal({confirmColor: '#eb4b4b',
            title: '温馨提示',
            content: "已存在未推送验工计价数据",
            showCancel: false,
          })
          return
        }
      }
      if (Number(this.item.sumMoney)==0){
        uni.showModal({confirmColor: '#eb4b4b',
          title: '温馨提示',
          content: "金额已经为0,不能冲销",
          showCancel: false,
        })
        return;
      }
      
      if (Number(this.item.sumNum) ==0 && this.isNew){
        uni.showModal({confirmColor: '#eb4b4b',
          title: '温馨提示',
          content: "数量已经为0,不能冲销",
          showCancel: false,
        })
        return;
      }
      uni.navigateTo({
        url: `./writeOff2?valuationId=${valuationId}&isNew=${this.isNew}&isEdit=false`
      });
    },
    chongxiaoNew(valuationId) {
      const xList = this.item.valuationDetails.filter(item=>item.payType=='FIRST')
      if (xList.length!=1||Number(xList[0]?.currentInspectionMoney)<=0){
        uni.showModal({confirmColor: '#eb4b4b',
          title: '温馨提示',
          content: "已冲销或数据异常,请勿重复点击",
          showCancel: false,
        })
        return;
      }
      const params = {
        prjId:this.item.subPrjId
      }
      postApi('/material/api/insVa/module/getAccInfo',{params}).then(res=>{
        if (res.data==0){
          uni.navigateTo({
            url: `./writeOff-new?valuationId=${valuationId}&isNew=${this.isNew}&isEdit=false`
          });
        }else{
          uni.showModal({confirmColor: '#eb4b4b',
            title: '温馨提示',
            content: '该项目已经过完工审核节点，请到物资服务绑定下操作',
            showCancel: false,
          })
        }
      })
      
    },
    deleteObj(e) {
      let that = this
      const detail = this.item.valuationDetails[this.item.valuationDetails.length-1]
      if (!detail){
        uni.showModal({confirmColor: '#eb4b4b',
          title: '温馨提示',
          content: "没有数据删除啦",
          showCancel: false,
        })
        return;
      }
      if (detail.isPush != '0'){
        uni.showModal({confirmColor: '#eb4b4b',
          title: '温馨提示',
          content: "已推送或已超时,无法删除",
          showCancel: false,
        })
        return;
      }
      uni.showModal({confirmColor: '#eb4b4b',
        title: '温馨提示',
        content: "是否确认删除",
        success: function(res1) {
          if (res1.confirm) {
            const data = {
              "onlyBe1Flag": that.item.onlyBe1Flag,
              "currentInspectionNum": Number(detail.currentInspectionNum),
              "currentInspectionMoney": Number(detail.currentInspectionMoney),
              "serviceCode": that.item.serviceCode,
              "prjId": that.item.prjId,
              "subPrjId": detail.subPrjId,
              "pId": detail.valuationId,
              "vId": detail.valuationDetailId,
              "flag": "3",
              "oldSumMoney": that.item.sumMoney,
              "oldSumNum": that.item.sumNum
            }
            postApi('/material/api/insVa/app/updateValDetails',{data}).then(res =>{
              uni.showModal({confirmColor: '#eb4b4b',
                title: '温馨提示',
                content: res.message,
                showCancel: false,
                success: function () {
                  that.$emit('change')
                }
              })
          
            })
          }
        }
      });
    },
    tuisong() {
      let that = this
      console.log(123,that.item)
      const detail = this.item.valuationDetails[this.item.valuationDetails.length-1]
      if (!detail){
        uni.showModal({confirmColor: '#eb4b4b',
          title: '温馨提示',
          content: "没有数据推送啦",
          showCancel: false,
        })
        return;
      }
      if (detail.isPush == '1'){
        uni.showModal({confirmColor: '#eb4b4b',
          title: '温馨提示',
          content: "已推送,请勿重复推送",
          showCancel: false,
        })
        return;
      }
      uni.showModal({confirmColor: '#eb4b4b',
        title: '温馨提示',
        content: "当前验工计价将推送商合交易",
        
        success: function(res1) {
          if (res1.confirm) {
            const type = Math.sign(detail.currentInspectionNum) === 1 ? 1 : 2
            const data = {
              prjId: that.item.prjId,
              insValProPushVOList:[
                {
                  vId: detail.valuationDetailId,
                  prjId: that.item.prjId,
                  subPrjId: detail.subPrjId,
                  serviceCode: that.item.serviceCode,
                  type:type,
                  orderNo:that.item.orderCode,
                }
              ]
            }
            postApi('/material/api/insVa/module/insValProPush',{data}).then(res =>{
              uni.showModal({confirmColor: '#eb4b4b',
                title: '温馨提示',
                content: res.message,
                showCancel: false,
                success: function() {
                  that.$emit('change')
                }
              })
            })
            // return
            // uni.request({
            //   url: uni.getStorageSync('url') +
            //     "/pms/app/service/pushValuationDetail?valuationDetailId=" + e,
            //   method: 'POST',
            //   dataType: 'json',
            //   timeout: 10000 * 60,
            //   header: {
            //     'pmsToken': uni.getStorageSync('pmsToken')
            //   },
            //   success: (res) => {
            //     uni.hideLoading();
            //     var data = res.data;
            //     var result = JSON.stringify(data)
            //     console.log('列表数据:' + result);
            //     if (data.resultStat == "000") {
            //       uni.showModal({confirmColor: '#eb4b4b',
            //         title: '温馨提示',
            //         content: data.message,
            //         showCancel: false,
            //         success: function(res) {
            //           if (res.confirm) {
            //             that.addlist = [];
            //             that.getList()
            //           }
            //         }
            //       })
            //     } else {
            //       uni.showModal({confirmColor: '#eb4b4b',
            //         title: '温馨提示',
            //         content: data.message,
            //         showCancel: false,
            //       })
            //     }
            //     uni.hideLoading();
            //     uni.stopPullDownRefresh();
            //   },
            //   fail: (data, code) => {
            //     uni.hideLoading();
            //     uni.stopPullDownRefresh();
            //     console.log('fail' + JSON.stringify(data));
            //   }
            // });
            
          }
        }
      });
    },
  }
}
</script>
<style lang='scss' scoped>
.store{
  font-size: 30rpx;
  font-weight: bold;
}
.type{
  font-size: 27rpx;
  color: #AFAEAC
}
.coll-l {
  width: 95%;
  background-color:#ffffff;
  margin: 20rpx auto;
  border-radius: 20rpx;
  box-sizing: border-box;
  overflow: hidden;
  .chanshishuju {
    width: 100%;
    color: black;
    text-align: left;
    padding: 10rpx 4% 10rpx 20rpx;
    
    .chanshishuju-to {
      width: 100%;
      display: flex;
      
      .none1 {
        width: 70%;
        display: flex;
        text-align: left;
        
        .yuancd {
          width: 60rpx;
          
          .yuan {
            width: 40rpx;
            height: 40rpx;
            text-align: center;
            border-radius: 25rpx;
            background-color: red;
            color: #FFFFFF;
            line-height: 40rpx;
            margin: 6rpx
          }
        }
      }
      
      .none2 {
        width: 30%;
        color: red;
        text-align: right
      }
    }
    
    .chanshishuju-buttom {
      width: 100%;
      color: #737373;
      display: flex;
      justify-content: space-between;
    }
    
  }
  
  .list-body {
    width: 100%;
    padding: 10rpx 4% 10rpx 80rpx;
    background-color: #00000010;
    margin: 0rpx;
    color: #737373;
    
    .yuanab {
      view {
        text-align: left;
        display: flex;
        width: 100%;
        overflow: hidden;
        
        .t1 {
          white-space: nowrap;
        }
      
      ;
        
        .t2 {
          width: 60%;
        }
      }
    }
    
  }
  
  .Slider {
    width: 100%;
    
    .list-bodyer {
      width: 100%;
      padding: 20rpx 6% 20rpx 80rpx;
      margin: 0rpx;
      color: #737373;
      background-color: white;
    }
    
    .Pricinginformation {
      width: 100%;
      height: 360rpx;
      border-top: #00000010 1rpx solid;
      
      .Pricingtion {
        width: 100%;
        border-top: white 1px solid;
        
        .lister-top {
          width: 100%;
          display: flex;
          padding: 10rpx 4% 10rpx 20rpx;
          font-weight: bold;
        }
        
        .lister-buttom {
          width: 100%;
          padding: 20rpx 6% 20rpx 80rpx;
          margin: 0rpx;
          color: #737373;
          background-color: white;
          .conner{
            width: 100%;
            display: flex;
            .t1{
              width: 48%;
            }
            .t2{
              width: 52%;
            }
          }
          .conner-buttom{
            width: 100%;
            display: flex;
            padding: 30rpx 8% 10rpx 0 ;
            view{
              margin: auto;
              width: 50%;
              text-align: center;
            }
          }
        }
      }
    }
    
    .btn-body {
      padding: 25rpx 0;
      display: flex;
      width: 100%;
      border-top: #00000010 0.5rpx solid;
    }
  }
}
</style>
